package cn.trigram.mgb.plugins;

import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.TopLevelClass;

/**
 * 生成Jackson的JsonProperty注解
 */
public class JacksonPlugin extends PluginAdapter {

  @Override
  public boolean validate(List<String> warnings) {

    return true;
  }

  @Override
  public boolean modelFieldGenerated(
      Field field, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn,
      IntrospectedTable introspectedTable, ModelClassType modelClassType
  ) {

    topLevelClass.addImportedType(new FullyQualifiedJavaType("com.fasterxml.jackson.annotation.JsonProperty"));
    field.addAnnotation(
        String.format("@JsonProperty(\"%s\")", StringUtils.lowerCase(introspectedColumn.getActualColumnName())));
    return true;
  }

}
